{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#请先运行此代码\n",
    "from IPython.core.interactiveshell import InteractiveShell\n",
    "InteractiveShell.ast_node_interactivity = \"all\" \n",
    "#这玩意可以让代码块分行输出"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 使用模块"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 编写一个hello的模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "' a test module '"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Too many arguments!\n"
     ]
    }
   ],
   "source": [
    "#!/usr/bin/env python3\n",
    "# -*- coding: utf-8 -*-\n",
    "\n",
    "' a test module '\n",
    "\n",
    "__author__ = '木青羽'\n",
    "\n",
    "import sys\n",
    "\n",
    "def test():\n",
    "    args = sys.argv#sys模块有一个argv变量，用list存储了命令行的所有参数。argv至少有一个元素，\n",
    "    #因为第一个参数永远是该.py文件的名称，例如：运行python3 hello.py获得的sys.argv就是['hello.py']；\n",
    "    #运行python3 hello.py Michael获得的sys.argv就是['hello.py', 'Michael']。\n",
    "    if len(args)==1:\n",
    "        print('Hello, world!')\n",
    "    elif len(args)==2:\n",
    "        print('Hello, %s!' % args[1])\n",
    "    else:\n",
    "        print('Too many arguments!')\n",
    "\n",
    "if __name__=='__main__':#当我们在命令行运行hello模块文件时，Python解释器把一个特殊变量__name__置为__main__，\n",
    "    #而如果在其他地方导入该hello模块时，if判断将失败，因此，这种if测试可以让一个模块通过命令行运行时执行一些额外的代码，最常见的就是运行测试。\n",
    "    test()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 作用域"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def _private_1(name):#类似_xxx和__xxx这样的函数或变量就是非公开的（private）\n",
    "    return 'Hello, %s' % name\n",
    "\n",
    "def _private_2(name):#类似_xxx和__xxx这样的函数或变量就是非公开的（private）\n",
    "    return 'Hi, %s' % name\n",
    "\n",
    "def greeting(name):#外部调用方法\n",
    "    if len(name) > 3:\n",
    "        return _private_1(name)\n",
    "    else:\n",
    "        return _private_2(name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 安装第三方模块"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 默认情况下，Python解释器会搜索当前目录、所有已安装的内置模块和第三方模块，搜索路径存放在sys模块的path变量中："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "2e918aaa81d99c652401bdd1a0c185581595fb477ac919641bd65261b5d7782a"
  },
  "kernelspec": {
   "display_name": "Python 3.9.7 64-bit ('base': conda)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
